﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MigrateAllPublicFolder
{
    class MyUtility
    {

        /// <summary>
        /// Return string formatted without sharepoint special charachters
        /// </summary>
        /// <param name="valCAmpo"></param>
        /// <returns></returns>
        public static string GetStringAllowedChars(String valCAmpo)
        {
            //Special Characters Not Allowed: ~ " # % & * : < > ? / \ { | }      

            string valTochange = valCAmpo;

            if (valTochange.Contains("/"))
                valTochange = valTochange.Replace("/", " ");
            if (valTochange.Contains("&"))
                valTochange = valTochange.Replace("&", " ");
            if (valTochange.Contains("°"))
                valTochange = valTochange.Replace("°", "");
            if (valTochange.Contains(":"))
                valTochange = valTochange.Replace(":", "");
            if (valTochange.Contains("#"))
                valTochange = valTochange.Replace("#", "");
            if (valTochange.Contains("%"))
                valTochange = valTochange.Replace("%", "");
            if (valTochange.Contains("*"))
                valTochange = valTochange.Replace("*", "");
            if (valTochange.Contains("<"))
                valTochange = valTochange.Replace("<", "");
            if (valTochange.Contains(">"))
                valTochange = valTochange.Replace(">", "");
            if (valTochange.Contains("?"))
                valTochange = valTochange.Replace("?", "");
            if (valTochange.Contains("{"))
                valTochange = valTochange.Replace("{", "");
            if (valTochange.Contains("|"))
                valTochange = valTochange.Replace("|", "");
            if (valTochange.Contains("}"))
                valTochange = valTochange.Replace("}", "");
            if (valTochange.Contains("\""))
                valTochange = valTochange.Replace("\"", "");
            if (valTochange.Contains("\\"))
                valTochange = valTochange.Replace("\\", " ");

            valTochange = RemoveInvalidEndingChars(valTochange);
            valTochange = valTochange.Trim();

            return valTochange;
        }


        /// <summary>
        /// Generate exadecimal code with specified length
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GenerateExadecimalCode(int length)
        {
            string Code = "";
            Random rnd = new Random();

            for (int i = 0; i < length; i++)
            {
                Code += "" + GetEsadecimal(rnd.Next(16));
            }

            return Code;
        }


        /// <summary>
        /// Get the corresponding exadecimal of a decinal number
        /// </summary>
        /// <param name="Decimal"></param>
        /// <returns></returns>
        public static string GetEsadecimal(int Decimal)
        {
            string Ex = "";

            if (Decimal > 9)
            {
                switch (Decimal.ToString())
                {
                    case "10": Ex = "A"; break;
                    case "11": Ex = "B"; break;
                    case "12": Ex = "C"; break;
                    case "13": Ex = "D"; break;
                    case "14": Ex = "E"; break;
                    case "15": Ex = "F"; break;
                }
            }
            else
                Ex = Decimal.ToString();

            return Ex;
        }


        /// <summary>
        /// Remove . - and blank from the end of the string
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string RemoveInvalidEndingChars(string str)
        {
            while (str.EndsWith(".") || str.EndsWith("-") || str.EndsWith(" "))
                str = str.Remove(str.Length - 1);

            return str;
        }


        /// <summary>
        /// Truncate string at char 250
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public static string GetMax250chars(string a)
        {
            if (a.Length > 250)
                a = a.Remove(250);

            return a;
        }


        /// <summary>
        /// Truncate string at char 100
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public static string GetMax100chars(string a)
        {
            if (a.Length > 100)
                a = a.Remove(100);

            return a;
        }

        
        /// <summary>
        /// Get relative path of a public folder from the absolute path
        /// </summary>
        /// <param name="completepath"></param>
        /// <returns></returns>
        public static string GetRelativePFPath(string completepath)
        {
            completepath = completepath.Replace("%2F", " ");

            string path = "";
            string[] a = completepath.Split('\\');

            for (int k = 5; k < a.Length; k++)
            {
                path += "/" + GetStringAllowedChars(a[k]);
            }

            return path;
        }


        /// <summary>
        /// Return a string with current date and time
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentDateAndTime()
        {
            DateTime Now = DateTime.Now;
            string Time = Now.Hour + ":" + Now.Minute + ":" + Now.Second;
            
            return Now.Date.ToString("dd/MM/yyyy") + " " + Time;
        }

    }
}
